﻿using System;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Organizer.Model
{
    [Table]
    public class User : Base<User>
    {
        [Column(IsPrimaryKey = true, DbType = "UniqueIdentifier NOT NULL", CanBeNull = false, AutoSync = AutoSync.OnInsert, UpdateCheck = UpdateCheck.Never)]
        public Guid Id { get; private set; }

        internal string _mail;
        [Column]
        public string Mail
        {
            get { return _mail; }
            set
            {
                if (_mail == value) return;
                NotifyPropertyChanging(() => this.Mail);
                _mail = value;
                NotifyPropertyChanged(() => this.Mail);
            }
        }

        internal string _passWord;
        [Column]
        public string PassWord
        {
            get { return _passWord; }
            set
            {
                if (_passWord == value) return;
                NotifyPropertyChanging(() => this.PassWord);
                _passWord = value;
                NotifyPropertyChanged(() => this.PassWord);
            }
        }

        [Column]
        internal Guid _manId;
        private EntityRef<Man> _man;
        [Association(Storage = "_man", ThisKey = "_manId", OtherKey = "Id", IsForeignKey = true)]
        public Man Man
        {
            get { return _man.Entity; }
            set
            {
                NotifyPropertyChanging(() => this.Man);
                _man.Entity = value;
                NotifyPropertyChanged(() => this.Man);
                if (value != null) _manId = value.Id;
            }
        }

        public User() { Id = Guid.NewGuid(); }

        public User(string login, string password, Man man)
            : this()
        {
            Mail = login;
            PassWord = password;
            Man = man;
        }

        private readonly EntitySet<Settings> _settingss = new EntitySet<Settings>();
        [Association(Storage = "_settingss", OtherKey = "_userId", ThisKey = "Id", DeleteRule = "CASCADE")]
        public EntitySet<Settings> Settingss
        {
            get { return _settingss; }
            set { _settingss.Assign(value); }
        }
    }
}
